package com.pdg.mcplugin.questfor.dataproviders;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.bukkit.Location;

import com.pdg.mcplugin.common.baseclasses.DataProviderBase;
import com.pdg.mcplugin.common.interfaces.TableProviderValue;


public class QuestInstance implements TableProviderValue<QuestInstance>{
	public static final String FIELD_LEVEL="level";
	public static final String FIELD_STATUS="status";
	public static final String FIELD_X="x";
	public static final String FIELD_Z="z";
	private static final String CREATE_FIELDS = "`"+FIELD_LEVEL+"` INT NOT NULL, `"+FIELD_STATUS+"` TEXT NOT NULL, `"+FIELD_X+"` REAL NOT NULL, `"+FIELD_Z+"` REAL NOT NULL";
	private static final String INSERT_FIELDS = "`"+FIELD_LEVEL+"`, `"+FIELD_STATUS+"`, `"+FIELD_X+"`, `"+FIELD_Z+"`";
	private static final String INSERT_VALUES_FORMAT = "%d, '%s', %f, %f";
	private static final String SELECT_LIST = "`"+FIELD_LEVEL+"`, `"+FIELD_STATUS+"`, `"+FIELD_X+"`, `"+FIELD_Z+"`";
	private static final String UPDATE_LIST_FORMAT = "`"+FIELD_LEVEL+"`=%d, `"+FIELD_STATUS+"`='%s', `"+FIELD_X+"`=%f, `"+FIELD_Z+"`=%f";
	private static final int DEFAULT_LEVEL = -1;
	private static final QuestStatus DEFAULT_STATUS = QuestStatus.COMPLETE;
	private static final double DEFAULT_X = 0;
	private static final double DEFAULT_Z = 0;

	private int level;
	private QuestStatus status;
	private double x;
	private double z;
	
	public QuestInstance(int theLevel,QuestStatus theStatus,double theX,double theZ){
		setLevel(theLevel);
		setStatus(theStatus);
		setX(theX);
		setZ(theZ);
	}

	public void setZ(double theZ) {
		z = theZ;
	}

	public void setX(double theX) {
		x = theX;
	}

	public void setStatus(QuestStatus theStatus) {
		status = theStatus;
	}

	public void setLevel(int theLevel) {
		level = theLevel;
	}

	public int getLevel() {
		return level;
	}

	public QuestStatus getStatus() {
		return status;
	}

	public double getX() {
		return x;
	}

	public double getZ() {
		return z;
	}

	@Override
	public String getCreateFields() {
		return CREATE_FIELDS;
	}

	@Override
	public String getInsertFields() {
		return INSERT_FIELDS;
	}

	@Override
	public String getInsertValues() {
		return String.format(INSERT_VALUES_FORMAT, getLevel(),DataProviderBase.sqlSafeString(getStatus().toString()),getX(),getZ());
	}

	@Override
	public String getSelectList() {
		return SELECT_LIST;
	}

	@Override
	public String getUpdateList() {
		return String.format(UPDATE_LIST_FORMAT, getLevel(),DataProviderBase.sqlSafeString(getStatus().toString()),getX(),getZ());
	}

	@Override
	public QuestInstance createValueFromResultSet(ResultSet theResultSet) {
		try {
			return new QuestInstance(theResultSet.getInt(FIELD_LEVEL), QuestStatus.valueOf(theResultSet.getString(FIELD_STATUS)), theResultSet.getDouble(FIELD_X), theResultSet.getDouble(FIELD_Z));
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static QuestInstance defaultInstance(){
		return new QuestInstance(DEFAULT_LEVEL,DEFAULT_STATUS,DEFAULT_X,DEFAULT_Z);
	}

	public double getDistance(Location location) {
		double deltaX = getX() - location.getX();
		double deltaZ = getZ() - location.getZ();
		return Math.sqrt(deltaX*deltaX+deltaZ*deltaZ);
	}
	public double getRelativeYaw(Location theLocation){
		double theDeltaX = theLocation.getX() - getX();
		double theDeltaZ = theLocation.getZ() - getZ();
		double theYaw = Math.atan2(theDeltaZ, theDeltaX) * 180 / Math.PI;
		
		double thePlayerYaw = theLocation.getYaw()+90+180;
		while(thePlayerYaw>180.0) thePlayerYaw-=360.0;
		while(thePlayerYaw<=-180.0) thePlayerYaw+=360.0;
		
		theYaw -= thePlayerYaw;
		while(theYaw>180.0) theYaw-=360.0;
		while(theYaw<=-180.0) theYaw+=360.0;
		return theYaw;
	}

}
